// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/03/2011 12:28:06 p.m.
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   ComprasDeImportacionPanel.java

package com.luxsoft.sw3.impap.ui.consultas;

import ca.odell.glazedlists.*;
import ca.odell.glazedlists.gui.TableFormat;
import com.luxsoft.siipap.compras.model.Compra2;
import com.luxsoft.siipap.compras.model.CompraUnitaria;
import com.luxsoft.siipap.model.Periodo;
import com.luxsoft.siipap.reportes.ComprasDepuradasReportForm;
import com.luxsoft.siipap.reportes.ComprasPendientesReportForm;
import com.luxsoft.siipap.swing.browser.AbstractMasterDatailFilteredBrowserPanel;
import com.luxsoft.siipap.swing.browser.FilteredBrowserPanel;
import com.luxsoft.siipap.swing.matchers.CheckBoxMatcher;
import com.luxsoft.siipap.swing.utils.*;
import java.util.List;
import javax.swing.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;
import org.jdesktop.swingx.table.TableColumnExt;

// Referenced classes of package com.luxsoft.sw3.impap.ui.consultas:
//            ComprasDeImportacionController

public class ComprasDeImportacionPanel extends AbstractMasterDatailFilteredBrowserPanel<Compra2,CompraUnitaria>
{

    public ComprasDeImportacionPanel()
    {
        super(Compra2.class);
        setTitle("Compras");
    }

    protected void init()
    {
        controller = new ComprasDeImportacionController();
        super.init();
    }

    protected void agregarMasterProperties()
    {
        addProperty(new String[] {
            "folio", "especial", "clave", "nombre", "fecha", "moneda", "tc", "total", "descuentoEspecial", "depuracion", 
            "consolidada", "comentario"
        });
        addLabels(new String[] {
            "Folio", "Esp", "Prov", "Nombre", "Fecha", "Mon", "TC", "Total", "Dscto", "Depuracion", 
            "Con", "Comentario"
        });
        setDetailTitle("Ordenes de Compra");
        installTextComponentMatcherEditor("Proveedor", new String[] {
            "clave", "nombre"
        });
        installTextComponentMatcherEditor("Folio", new String[] {
            "folio"
        });
        manejarPeriodo();
    }

    protected void manejarPeriodo()
    {
        periodo = Periodo.getPeriodoConAnteriroridad(6);
    }

    protected ca.odell.glazedlists.CollectionList.Model createPartidasModel()
    {
        return new ca.odell.glazedlists.CollectionList.Model() {

            public List getChildren(Compra2 parent)
            {
                return controller.buscarPartidas(parent);
            }

            public  List getChildren(Object obj)
            {
                return getChildren((Compra2)obj);
            }

            
        }
;
    }

    protected TableFormat createDetailTableFormat()
    {
        String props[] = {
            "compra.folio", "sucursalNombre", "clave", "descripcion", "unidad", "solicitado", "recibido", "depurado", "pendiente", "depuracion", 
            "folioOrigen", "comentario"
        };
        String labels[] = {
            "Folio", "Sucursal", "Prod", "Descrpci\363n", "U", "Solicitado", "Recibido", "Depurado", "Pendiente", "Depuracion", 
            "Origen", "Comentario"
        };
        return GlazedLists.tableFormat(CompraUnitaria.class, props, labels);
    }

    protected void adjustMainGrid(JXTable grid)
    {
        grid.getColumnExt("Depuracion").setCellRenderer(new DefaultTableRenderer(new com.luxsoft.siipap.swing.utils.Renderers.ToHourConverter()));
    }

    public Action[] getActions()
    {
        if(actions == null)
        {
            getInsertAction().putValue("SmallIcon", ResourcesUtils.getIconFromResource("images2/basket_add.png"));
            getEditAction().putValue("SmallIcon", ResourcesUtils.getIconFromResource("images2/basket_edit.png"));
            printAction = addContextAction(new com.luxsoft.siipap.swing.browser.FilteredBrowserPanel.NotNullSelectionPredicate(), "", "imprimir", "Imprimir");
            printAction.putValue("SmallIcon", ResourcesUtils.getIconFromResource("images/file/printview_tsk.gif"));
            actions = (new Action[] {
                getLoadAction(), getViewAction(), getInsertAction(), getEditAction(), printAction, addAction("", "cancelar", "Cancelar"), addAction("", "depurar", "Depurar"), addAction("", "importar", "Importar"), addAction("", "cambiarProveedor", "Cambiar proveedor")
            });
        }
        return actions;
    }

    protected List createProccessActions()
    {
        List procesos = super.createProccessActions();
        procesos.add(addAction("", "reporteComprasDepuradas", "Compras Depuradas"));
        procesos.add(addAction("", "reporteComprasPendientes", "Compras Pendientes"));
        return procesos;
    }

    public JComponent[] getOperacionesComponents()
    {
        if(pendientesBox == null)
        {
            pendientesBox = new JCheckBox("Pendientes", true);
            pendientesBox.setOpaque(false);
        }
        return (new JComponent[] {
            pendientesBox
        });
    }

    protected List findData()
    {
        if(pendientesBox.isSelected())
            return controller.buscarPendientes();
        else
            return controller.buscarCompras(periodo);
    }

    protected void executeLoadWorker(SwingWorker worker)
    {
        TaskUtils.executeSwingWorker(worker);
    }

    protected Compra2 doInsert()
    {
        return controller.generarCompraDeImportacion();
    }

    protected void afterInsert(Compra2 bean)
    {
        super.afterInsert(bean);
        imprimir(bean);
    }

    protected Compra2 doEdit(Compra2 bean)
    {
        return controller.editar(bean);
    }

    protected void doSelect(Object bean)
    {
        Compra2 selected = (Compra2)bean;
        controller.mostrarCompra(selected.getId());
    }

    public void importar()
    {
        String val = JOptionPane.showInputDialog("Folio: ");
        if(StringUtils.isNotBlank(val) && NumberUtils.isNumber(val))
        {
            Long folio = new Long(val);
            Compra2 compra = controller.importarCompra(folio);
            source.add(compra);
        }
    }

    public void cambiarProveedor()
    {
        Compra2 selected = (Compra2)getSelectedObject();
        if(selected != null)
        {
            int index = source.indexOf(selected);
            if(index != -1)
            {
                Compra2 res = controller.cambiarProveedor(selected);
                if(res != null)
                    source.set(index, res);
            }
        }
    }

    public void cancelar()
    {
        Compra2 res = controller.cancelar(getSelectedObject());
        if(res != null)
        {
            int index = source.indexOf(res);
            if(index != -1)
                source.set(index, res);
        }
    }

    public void depurar()
    {
        Compra2 res = controller.depurar(getSelectedObject());
        if(res != null)
        {
            int index = source.indexOf(res);
            if(index != -1)
                source.set(index, res);
        }
    }

    public void imprimir()
    {
        executeSigleSelection(new com.luxsoft.siipap.swing.browser.FilteredBrowserPanel.SingleSelectionHandler() {

            public Compra2 execute(Compra2 selected)
            {
                ComprasDeImportacionController.imprimir(selected);
                return selected;
            }

            public  Object execute(Object obj)
            {
                return execute((Compra2)obj);
            }

           
        }
);
    }

    public void imprimir(Compra2 c)
    {
        ComprasDeImportacionController.imprimir(c);
    }

    public void reporteComprasDepuradas()
    {
        ComprasDepuradasReportForm.run();
    }

    public void reporteComprasPendientes()
    {
        ComprasPendientesReportForm.run();
    }




    ComprasDeImportacionController controller;
    CheckBoxMatcher nacionalesMatcher;
    CheckBoxMatcher importadosMatcher;
    private Action printAction;
    private JCheckBox pendientesBox;
}
